{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find Anagram Mappings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: anagramMappings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #找出变位映射"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定两个列表 <code>A</code>and <code>B</code>，并且 <code>B</code> 是 <code>A</code> 的变位（即&nbsp;<code>B</code> 是由&nbsp;<code>A</code> 中的元素随机排列后组成的新列表）。</p>\n",
    "\n",
    "<p>我们希望找出一个从 <code>A</code> 到 <code>B</code>&nbsp;的索引映射 <code>P</code> 。一个映射 <code>P[i] = j</code>&nbsp;指的是列表&nbsp;<code>A</code> 中的第 <code>i</code> 个元素出现于列表&nbsp;<code>B</code> 中的第 <code>j</code> 个元素上。</p>\n",
    "\n",
    "<p>列表 <code>A</code> 和 <code>B</code> 可能出现重复元素。如果有多于一种答案，输出任意一种。</p>\n",
    "\n",
    "<p>例如，给定</p>\n",
    "\n",
    "<pre>A = [12, 28, 46, 32, 50]\n",
    "B = [50, 12, 32, 46, 28]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p>需要返回</p>\n",
    "\n",
    "<pre>[1, 4, 3, 2, 0]\n",
    "</pre>\n",
    "\n",
    "<p><code>P[0] = 1</code>&nbsp;，因为 <code>A</code> 中的第 <code>0</code> 个元素出现于 <code>B[1]</code>，而且 <code>P[1] = 4</code> 因为 <code>A</code> 中第 <code>1</code> 个元素出现于 <code>B[4]</code>，以此类推。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>注：</strong></p>\n",
    "\n",
    "<ol>\n",
    "\t<li><code>A, B</code>&nbsp;有相同的长度，范围为&nbsp;<code>[1, 100]</code>。</li>\n",
    "\t<li><code>A[i], B[i]</code> 都是范围在&nbsp;<code>[0, 10^5]</code> 的整数。</li>\n",
    "</ol>\n",
    "\n",
    "<p>&nbsp;</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-anagram-mappings](https://leetcode.cn/problems/find-anagram-mappings/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-anagram-mappings](https://leetcode.cn/problems/find-anagram-mappings/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[12,28,46,32,50]\\n[50,12,32,46,28]', '[84,46]\\n[84,46]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        hash_dict = {}\n",
    "        for index, num in enumerate(nums2):\n",
    "            hash_dict[num] = index\n",
    "        res = []\n",
    "        for num in nums1:\n",
    "            res.append(hash_dict[num])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        n = len(nums1)\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if nums1[i] == nums2[j]:\n",
    "                    res.append(j)\n",
    "                    break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        position_dict = {}\n",
    "        for idx, num in enumerate(nums2):\n",
    "            position_dict[num] = idx\n",
    "\n",
    "        res = []\n",
    "        for num in nums1:\n",
    "            res.append(position_dict[num])\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        p = []\n",
    "        for i in range(0, len(nums1)):\n",
    "            for j in range(0, len(nums2)):\n",
    "                if nums2[j] == nums1[i]:\n",
    "                    if j in p:\n",
    "                        continue\n",
    "                    else:\n",
    "                        p.append(j)\n",
    "                        break\n",
    "        \n",
    "        return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        D = {x: i for i, x in enumerate(nums2)}\n",
    "        return [D[x] for x in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        d = defaultdict(list)\n",
    "        for i, ele in enumerate(nums2):\n",
    "            d[ele].append(i)\n",
    "        ans = []\n",
    "        for i, ele in enumerate(nums1):\n",
    "            idx = d[ele].pop()\n",
    "            ans.append(idx)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        n =len(nums1)\n",
    "        p = []\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if nums1[i] == nums2[j]:\n",
    "                    p.append(j)\n",
    "                    break\n",
    "        return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        mapping = []\n",
    "        for i in range(len(nums1)):\n",
    "            num = nums1[i]\n",
    "            pos = nums2.index(num)\n",
    "            while pos in mapping:\n",
    "                pos = nums2[pos+1:].index(num) + pos + 1\n",
    "            mapping.append(pos)\n",
    "        return mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        mp = {num:i for i,num in enumerate(nums2)}\n",
    "        ans = []\n",
    "        for num in nums1:\n",
    "            ans.append(mp[num])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        D = {x: i for i, x in enumerate(nums2)}\n",
    "        return [D[x] for x in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        for num in nums1:\n",
    "            res.append(nums2.index(num))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        Hash = {}\n",
    "        for i, num in enumerate(nums2):\n",
    "            Hash[num] = i\n",
    "        for num in nums1:\n",
    "            res.append(Hash[num])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        for x in nums1:\n",
    "            res.append(nums2.index(x))\n",
    "        \n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "      dic = {num: idx for idx, num in enumerate(nums2)}\n",
    "      return [dic[num] for num in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from functools import cache\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        dictionary = {}\n",
    "        for index in range(len(nums2)):\n",
    "            dictionary[nums2[index]] = index\n",
    "\n",
    "        return [dictionary[num] for num in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        n = len(nums1)\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if nums1[i] == nums2[j] and j not in res:\n",
    "                    res.append(j)\n",
    "                    break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        nums2_pos_dict = defaultdict(list)\n",
    "        for i, num in enumerate(nums2):\n",
    "            nums2_pos_dict[num].append(i)\n",
    "        \n",
    "        for _, num in enumerate(nums1):\n",
    "            if nums2_pos_dict[num]:\n",
    "                res.append(nums2_pos_dict[num][-1])\n",
    "                nums2_pos_dict[num].pop()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        ct=defaultdict(list)\n",
    "        for i,num in enumerate(nums2):\n",
    "            ct[num].append(i)\n",
    "\n",
    "        ans=[]\n",
    "        for i in range(len(nums1)):\n",
    "            tm=ct[nums1[i]].pop()\n",
    "            ans.append(tm)\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        table = {}\n",
    "        for i in range(len(nums2)):\n",
    "            item = nums2[i]\n",
    "            if item not in table:\n",
    "                table[item] = i\n",
    "            else:\n",
    "                if isinstance(table[item], list):\n",
    "                    table[item].append(i)\n",
    "                else:\n",
    "                    table[item] = [table[item], i]\n",
    "        res = []\n",
    "        for j in range(len(nums1)):\n",
    "            if isinstance(table[nums1[j]], int):\n",
    "                res.append(table[nums1[j]])\n",
    "            else:\n",
    "                res.append(table[nums1[j]][0])\n",
    "                table[nums1[j]] = table[nums1[j]][1:]\n",
    "                if len(table[nums1[j]]) == 1:\n",
    "                    table[nums1[j]] = table[nums1[j]][-1]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        potential_position = list(range(len(nums2)))\n",
    "        P = []\n",
    "        for i in nums1:\n",
    "            for idx, pos in enumerate(potential_position):\n",
    "                if nums2[pos] == i:\n",
    "                    P.append(pos)\n",
    "                    potential_position.pop(idx)\n",
    "                    break\n",
    "        return P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        dt1,dt2=dict(),dict()\n",
    "        l=len(nums1)\n",
    "        for i in range(l):\n",
    "            # if nums1[i] not in dt1: dt1[nums1[i]]=[i]\n",
    "            # else: dt1[nums1[i]].add(i)\n",
    "            if nums2[i] not in dt2: dt2[nums2[i]]=[i]\n",
    "            else: dt2[nums2[i]].append(i)\n",
    "        ans=[]\n",
    "        for i in range(l):\n",
    "            t=nums1[i]\n",
    "            if len( dt2[t])==0: ans.append(-1)\n",
    "            else:\n",
    "                ans.append(dt2[t].pop())\n",
    "        return ans\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def anagramMappings(self, A, B):\n",
    "        D = {x: i for i, x in enumerate(B)}\n",
    "        return [D[x] for x in A]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        map_d = defaultdict(list)\n",
    "        out = []\n",
    "        for idx,ni in enumerate(nums2):\n",
    "            map_d[ni].append(idx)\n",
    "\n",
    "        for ni in nums1:\n",
    "            out.append(map_d[ni].pop(0))\n",
    "\n",
    "        return out\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        return [nums2.index(x) for x in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        \n",
    "        d_b={}\n",
    "        res=[]\n",
    "        for i in range(len(nums2)):\n",
    "            d_b[nums2[i]]=i\n",
    "        \n",
    "        for i in range(len(nums1)):\n",
    "            res.append(d_b[nums1[i]])\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        n = len(nums1)\n",
    "        ans = [0] * n\n",
    "        mapping = defaultdict(lambda: -1)\n",
    "\n",
    "        for i, num in enumerate(nums1):\n",
    "            ans[i] = nums2.index(num, mapping[num] + 1)\n",
    "            mapping[num] = ans[i]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        n = len(nums1)\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if nums1[i] == nums2[j]:\n",
    "                    res.append(j)\n",
    "                    break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        maps = defaultdict(list)\n",
    "        for i, num in enumerate(nums2):\n",
    "            maps[num].append(i)\n",
    "        \n",
    "        return [maps[num].pop() for num in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        D = {x:i for i ,x in enumerate(nums2)}\n",
    "        return [D[x] for x in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        for i in range(len(nums1)):\n",
    "            for j in range(len(nums2)):\n",
    "                if nums1[i] == nums2[j]:\n",
    "                    res.append(j)\n",
    "                    break\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        hashmap = {}\n",
    "        for i in range(len(nums2)):\n",
    "            hashmap[nums2[i]] = i\n",
    "        index = []\n",
    "        for item in nums1:\n",
    "            index.append(hashmap[item])\n",
    "        return index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        result = []\n",
    "        flag = [False] * len(nums1)\n",
    "\n",
    "        for i, num_i in enumerate(nums1):\n",
    "            for j, num_j in enumerate(nums2):\n",
    "                if num_i == num_j and not flag[j]:\n",
    "                    result.append(j)\n",
    "                    flag[j] =True\n",
    "                    break\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        p=[0]*len(nums1)\n",
    "        # print(p)\n",
    "        for i in range(len(nums1)):\n",
    "            for j in range(len(nums2)):\n",
    "                if nums2[j]==nums1[i]:\n",
    "                    p[i]=j\n",
    "        # print(p)\n",
    "        return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        dic = {}\n",
    "        res = []\n",
    "        for i in range(len(nums2)):\n",
    "            if nums2[i] in dic:\n",
    "                dic[nums2[i]].append(i)\n",
    "            else:\n",
    "                dic[nums2[i]] = [i]\n",
    "        for i in range(len(nums1)):\n",
    "            res.append(dic[nums1[i]].pop())\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        return [nums2.index(n) for n in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        n = len(nums1)\n",
    "        index = range(n)\n",
    "        a = sorted(zip(nums1, index), key = lambda x: x[0])\n",
    "        b = sorted(zip(nums2, index), key = lambda x: x[0])\n",
    "        c = sorted(zip(a, b), key = lambda x: x[0][1])\n",
    "        return [x[1][1] for x in c]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        hashmap,lis={},[]\n",
    "        for i in range(len(nums2)):\n",
    "            hashmap[nums2[i]]=i\n",
    "        for i in range(len(nums1)):\n",
    "            lis.append(hashmap[nums1[i]])\n",
    "        return lis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        p=[0]*len(nums1)\n",
    "        # print(p)\n",
    "        for i in range(len(nums1)):\n",
    "            for j in range(len(nums2)):\n",
    "                if nums2[j]==nums1[i]:\n",
    "                    p[i]=j\n",
    "        # print(p)\n",
    "        return p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        P = []\n",
    "        for i in nums1:\n",
    "            for j in range(len(nums2)):\n",
    "                if nums2[j] == i:\n",
    "                    P.append(j)\n",
    "                    break\n",
    "        return P "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        mp = {x:i for i, x in enumerate(nums2)}\n",
    "        return [mp[x] for x in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        dic = {}\n",
    "        res = []\n",
    "        for idx, val in enumerate(nums2):\n",
    "            dic[val] = idx\n",
    "        for num in nums1:\n",
    "            res.append(dic[num])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        m = {k: i for i, k in enumerate(nums2)}\n",
    "\n",
    "        return [m[k] for k in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        m = {k: i for i, k in enumerate(nums2)}\n",
    "\n",
    "        return [m[k] for k in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        ans=[]\n",
    "        for i in range(len(nums1)):\n",
    "            if len(ans)==len(nums1):\n",
    "                break\n",
    "            for j in range(len(nums2)):  \n",
    "                if nums1[i]==nums2[j]:\n",
    "                    ans.append(j)\n",
    "                    break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        dictionary = {}\n",
    "        for index in range(len(nums2)):\n",
    "            dictionary[nums2[index]] = index\n",
    "\n",
    "        return [dictionary[num] for num in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        dic = {}\n",
    "        res = []\n",
    "        for i in range(len(nums2)):\n",
    "            n = nums2[i]\n",
    "            dic[n] = dic.get(n, []) + [i]\n",
    "        for n1 in nums1:\n",
    "            res.append(dic[n1].pop())\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        e=collections.defaultdict(int)\n",
    "        for i,n in enumerate(nums2):\n",
    "            e[n]=i\n",
    "        ans=[]\n",
    "        for n in nums1:\n",
    "            ans.append(e[n])\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        hashmap = {}\n",
    "        for i in range(len(nums2)):\n",
    "            hashmap[nums2[i]] = i\n",
    "        index = []\n",
    "        for item in nums1:\n",
    "            index.append(hashmap[item])\n",
    "        return index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        my_dict = {}\n",
    "        for i, num in enumerate(nums2):\n",
    "            my_dict[num] = i\n",
    "        res = []\n",
    "        for num in nums1:\n",
    "            res.append(my_dict[num])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        cnt = {}\n",
    "        for i, num in enumerate(nums2):\n",
    "            if num not in cnt:\n",
    "                cnt[num]=[i]\n",
    "            else:\n",
    "                cnt[num].append(i)\n",
    "        res = []\n",
    "        for num in nums1:\n",
    "            res.append(cnt[num][0])\n",
    "            cnt[num].pop(0)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        mapper = {num: index for index, num in enumerate(nums2)}\n",
    "        return [mapper[num] for num in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        return [nums2.index(n) for n in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        D = {x: i for i, x in enumerate(nums2)}\n",
    "        return [D[x] for x in nums1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        idx2 = {}\n",
    "        ret = []\n",
    "        for i, n in enumerate(nums2):\n",
    "            if not n in idx2:\n",
    "                idx2[n] = {i}\n",
    "            else:\n",
    "                idx2[n].add(i)\n",
    "        for n in nums1:\n",
    "            j = idx2[n].pop()\n",
    "            ret.append(j)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        result = []\n",
    "        for n in nums1:\n",
    "            result.append(nums2.index(n))\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        for i in range(len(nums1)):\n",
    "            key = nums2.index(nums1[i])\n",
    "            res.append(key)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:\n",
    "        d2 = {}\n",
    "        l = len(nums2)\n",
    "        ans = []\n",
    "        for i in range(l):\n",
    "            d2[nums2[i]] = i\n",
    "        for num in nums1:\n",
    "            ans.append(d2[num])\n",
    "        return ans\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
